package com.hmy.contract.manager.signature;

import cn.hutool.json.JSONUtil;
import com.hmy.ccp.common.result.FeignResult;
import com.hmy.ccp.error.annotations.HmyFeignController;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.infrastructure.api.bo.request.signature.*;
import com.hmy.infrastructure.api.bo.response.signature.*;
import com.hmy.infrastructure.api.client.signature.SignatureClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author seven.qi 齐胜南
 * @version 1.0
 * @description 电子签相关接口
 * @date 2023/12/28 19:33
 */

@Component
@Slf4j
@HmyFeignController
public class SignatureFeignClientManager {

    @Resource
    private SignatureClient signatureClient;

    /**
     * 创建流程
     *
     * @param createSignatureProcessRequest 创建流程入参
     * @return {@link CreateSignatureProcessApiResponse}
     */
    public FeignResult<CreateSignatureProcessApiResponse> create(CreateSignatureProcessApiRequest createSignatureProcessRequest) {
        FeignResult<CreateSignatureProcessApiResponse> response = null;
        log.info("SignatureFeignClientRpc.create params:{}", JSONUtil.toJsonPrettyStr(createSignatureProcessRequest));
        response = signatureClient.create(createSignatureProcessRequest);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000008);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SignatureFeignClientRpc.create response:{}", JSONUtil.toJsonPrettyStr(response));
        return response;
    }

    /**
     * 创建合同，通用接口
     *
     * @param createContractApiRequest 创建流程入参
     * @return {@link CreateSignatureProcessApiResponse}
     */
    public CreateContractApiResponse createContract(CreateContractApiRequest createContractApiRequest) {
        FeignResult<CreateContractApiResponse> response = null;
        log.info("SignatureFeignClientRpc.createContract params:{}", JSONUtil.toJsonPrettyStr(createContractApiRequest));
        response = signatureClient.createContract(createContractApiRequest);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000008);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SignatureFeignClientRpc.createContract response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }

    /**
     * 签约模板数据填充
     *
     * @param fieldFillRequest 签约模板数据填充入参
     * @return {@link SignTemplateFieldFillApiResponse}
     */
    public FeignResult<SignTemplateFieldFillApiResponse> signTemplateFieldFill(SignTemplateFieldFillApiRequest fieldFillRequest) {
        FeignResult<SignTemplateFieldFillApiResponse> response = null;
        log.info("SignatureFeignClientRpc.signTemplateFieldFill params:{}", JSONUtil.toJsonPrettyStr(fieldFillRequest));
        response = signatureClient.signTemplateFieldFill(fieldFillRequest);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000009);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SignatureFeignClientRpc.signTemplateFieldFill response:{}", JSONUtil.toJsonPrettyStr(response));
        return response;
    }

    /**
     * 文件模板数据填充
     *
     * @param fileTemplateFieldFillRequest 文件模板数据填充入参
     * @return {@link FileTemplateFieldFillApiResponse}
     */
    public FeignResult<FileTemplateFieldFillApiResponse> fileTemplateFieldFill(FileTemplateFieldFillApiRequest fileTemplateFieldFillRequest) {
        FeignResult<FileTemplateFieldFillApiResponse> response = null;
        log.info("SignatureFeignClientRpc.fileTemplateFieldFill params:{}", JSONUtil.toJsonPrettyStr(fileTemplateFieldFillRequest));
        response = signatureClient.fileTemplateFieldFill(fileTemplateFieldFillRequest);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000010);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SignatureFeignClientRpc.fileTemplateFieldFill response:{}", JSONUtil.toJsonPrettyStr(response));
        return response;
    }

    /**
     * 开始签约流程
     *
     * @param startSignatureProcessRequest 开始签约流程入参
     * @return {@link StartSignatureProcessApiResponse}
     */
    public FeignResult<StartSignatureProcessApiResponse> start(StartSignatureProcessApiRequest startSignatureProcessRequest) {
        FeignResult<StartSignatureProcessApiResponse> response = null;
        log.info("SignatureFeignClientRpc.start params:{}", JSONUtil.toJsonPrettyStr(startSignatureProcessRequest));
        response = signatureClient.start(startSignatureProcessRequest);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000011);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SignatureFeignClientRpc.start response:{}", JSONUtil.toJsonPrettyStr(response));
        return response;
    }

    /**
     * 获取签署完成后的文件链接
     *
     * @param getSignFileUrlRequest 获取签署完成后的文件链接入参
     * @return {@link GetSignFileUrlApiResponse}
     */
    public FeignResult<GetSignFileUrlApiResponse> getSignFileUrl(GetSignFileUrlApiRequest getSignFileUrlRequest) {
        FeignResult<GetSignFileUrlApiResponse> response = null;
        log.info("SignatureFeignClientRpc.getSignFileUrl params:{}", JSONUtil.toJsonPrettyStr(getSignFileUrlRequest));
        response = signatureClient.getSignFileUrl(getSignFileUrlRequest);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000012);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SignatureFeignClientRpc.getSignFileUrl response:{}", JSONUtil.toJsonPrettyStr(response));
        return response;
    }

    /**
     * 获取签约链接
     *
     * @param getSignTaskUrlRequest 获取签约链接入参
     * @return {@link GetSignTaskUrlApiResponse}
     */
    public FeignResult<GetSignTaskUrlApiResponse> getSignTaskUrl(GetSignTaskUrlApiRequest getSignTaskUrlRequest) {
        FeignResult<GetSignTaskUrlApiResponse> response = null;
        log.info("SignatureFeignClientRpc.getSignTaskUrl params:{}", JSONUtil.toJsonPrettyStr(getSignTaskUrlRequest));
        response = signatureClient.getSignTaskUrl(getSignTaskUrlRequest);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000013);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SignatureFeignClientRpc.getSignTaskUrl response:{}", JSONUtil.toJsonPrettyStr(response));
        return response;
    }

    /**
     * 创建合同，通用接口
     *
     * @param createContractRequest 创建流程入参
     * @return {@link CreateSignatureProcessApiResponse}
     */
    public CreateContractByFileApiResponse createContractByFile(CreateContractByFileApiRequest createContractRequest) {
        FeignResult<CreateContractByFileApiResponse> response = null;
        log.info("SignatureFeignClientRpc.createContractByFile params:{}", JSONUtil.toJsonPrettyStr(createContractRequest));
        response = signatureClient.createContractByFile(createContractRequest);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000008);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SignatureFeignClientRpc.createContract response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }

    /**
     * 创建合同，通用接口
     *
     * @param request 创建流程入参
     * @return {@link SignTaskCancelApiResponse}
     */
    public SignTaskCancelApiResponse cancel(SignTaskCancelApiRequest request) {
        FeignResult<SignTaskCancelApiResponse> response = null;
        log.info("SignatureFeignClientRpc.cancel params:{}", JSONUtil.toJsonPrettyStr(request));
        response = signatureClient.cancel(request);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000008);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SignatureFeignClientRpc.cancel response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }

}
